package me.hetong.caigou.util;

import cn.hutool.core.collection.CollUtil;
import javafx.scene.image.ImageView;
import me.hetong.caigou.entity.Chess;
import me.hetong.caigou.entity.Point;
import me.hetong.caigou.entity.Zhao;
import me.hetong.caigou.ui.chessboard.ChessboardWarImage;

import java.util.ArrayList;
import java.util.List;

public class ChessUtils {
    public static List<Point> canMove(Chess chess) {
        if (chess == null) {
            return null;
        }

        List<Point> list = new ArrayList<>();

        switch (chess.getK().toLowerCase()) {
            case "r": {
                che(chess, list);
                break;
            }
            case "n": {
                ma(chess, list);
                break;
            }
            case "b": {
                xiang(chess, list);
                break;
            }
            case "a": {
                shi(chess, list);
                break;
            }
            case "k": {
                jiang(chess, list);
                break;
            }
            case "c": {
                pao(chess, list);
                break;
            }
            case "p": {
                bing(chess, list);
                break;
            }
            default:
                return null;
        }

        if (list.size() > 0) {
            ChessboardWarImage.clearFromLocation();
            for (Point point : list) {
                ImageView imageView = ChessboardWarImage.createImageView("from", point.getX(), point.getY());
                ChessboardWarImage.fromTempList.add(imageView);
            }
        }

        return list;
    }

    public static boolean moveValidate(Chess chess, Zhao zhao) {
        List<Point> list = canMove(chess);

        if (CollUtil.isEmpty(list)) {
            return false;
        }

        for (Point point : list) {
            if (point.getX() == zhao.getX2() && point.getY() == zhao.getY2()) {
                return true;
            }
        }
        return false;
    }

    private static void bing(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();

        int[][] arr;
        if (chess.getType().equals("b")) {
            if (chess.getY() <= 4) {
                arr = new int[][]{{0, 1}};
            } else {
                arr = new int[][]{{0, 1}, {-1, 0}, {1, 0}};
            }
        } else {
            if (chess.getY() >= 5) {
                arr = new int[][]{{0, -1}};
            } else {
                arr = new int[][]{{0, -1}, {-1, 0}, {1, 0}};
            }
        }
        for (int[] ints : arr) {

            int m = x + ints[0];
            int n = y + ints[1];

            Chess temp = ChessboardWarImage.getChess(m, n);
            if (temp == null || !chess.getType().equals(temp.getType())) {
                list.add(new Point(m, n));
            }
        }
    }

    private static void jiang(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();
        int[][] arr = {{-1, 0}, {0, -1}, {1, 0}, {0, 1}};
        for (int[] ints : arr) {
            int m = x + ints[0];
            int n = y + ints[1];

            if (m < 3 || m > 5) {
                continue;
            }

            if (chess.getType().equals("w") && (n < 7 || n > 9)) {
                continue;
            }

            if (chess.getType().equals("b") && (n > 2 || n < 0)) {
                continue;
            }

            Chess temp = ChessboardWarImage.getChess(m, n);
            if (temp == null || !chess.getType().equals(temp.getType())) {
                list.add(new Point(m, n));
            }
        }
    }

    private static void shi(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();
        int[][] arr = {{-1, -1}, {1, -1}, {1, 1}, {-1, 1}};
        for (int[] ints : arr) {
            int m = x + ints[0];
            int n = y + ints[1];

            if (m < 3 || m > 5) {
                continue;
            }

            if (chess.getType().equals("w") && (n < 7 || n > 9)) {
                continue;
            }

            if (chess.getType().equals("b") && (n > 2 || n < 0)) {
                continue;
            }

            Chess temp = ChessboardWarImage.getChess(m, n);
            if (temp == null || !chess.getType().equals(temp.getType())) {
                list.add(new Point(m, n));
            }
        }
    }

    private static void xiang(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();
        int[][] arr = {{-2, -2, -1, -1}, {2, -2, 1, -1}, {2, 2, 1, 1}, {-2, 2, -1, 1}};
        for (int[] ints : arr) {
            int m = x + ints[0];
            int n = y + ints[1];
            if (m < 0 || m > 8 || n < 0 || n > 9) {
                continue;
            }

            if (chess.getType().equals("w") && n < 5) {
                continue;
            }

            if (chess.getType().equals("b") && n > 4) {
                continue;
            }

            // 判断是否蹩脚
            int p = x + ints[2];
            int q = y + ints[3];
            if (ChessboardWarImage.getChess(p, q) != null) {
                continue;
            }

            Chess temp = ChessboardWarImage.getChess(m, n);
            if (temp == null || !chess.getType().equals(temp.getType())) {
                list.add(new Point(m, n));
            }
        }
    }

    private static void ma(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();
        // {-2, -1, -1, 0} 分别代表可走的目标位置变量x,，目标位置变量y，判断蹩脚的x和y
        int[][] arr = {{-2, -1, -1, 0}, {-1, -2, 0, -1}, {1, -2, 0, -1}, {2, -1, 1, 0}, {2, 1, 1, 0}, {1, 2, 0, 1}, {-1, 2, 0, 1}, {-2, 1, -1, 0}};
        for (int[] ints : arr) {
            int m = x + ints[0];
            int n = y + ints[1];
            if (m < 0 || m > 8 || n < 0 || n > 9) {
                continue;
            }

            // 判断是否蹩脚
            int p = x + ints[2];
            int q = y + ints[3];
            if (ChessboardWarImage.getChess(p, q) != null) {
                continue;
            }

            Chess temp = ChessboardWarImage.getChess(m, n);
            if (temp == null || !chess.getType().equals(temp.getType())) {
                list.add(new Point(m, n));
            }
        }
    }

    private static void che(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();

        for (int i = x + 1; i <= 8; i++) {
            Chess temp = ChessboardWarImage.getChess(i, y);
            if (temp == null) {
                list.add(new Point(i, y));
            } else {
                if (temp.getType().equals(chess.getType())) {
                    break;
                } else {
                    list.add(temp);
                    break;
                }
            }
        }

        for (int i = x - 1; i >= 0; i--) {
            Chess temp = ChessboardWarImage.getChess(i, y);
            if (temp == null) {
                list.add(new Point(i, y));
            } else {
                if (temp.getType().equals(chess.getType())) {
                    break;
                } else {
                    list.add(temp);
                    break;
                }
            }
        }

        for (int i = y + 1; i <= 9; i++) {
            Chess temp = ChessboardWarImage.getChess(x, i);
            if (temp == null) {
                list.add(new Point(x, i));
            } else {
                if (temp.getType().equals(chess.getType())) {
                    break;
                } else {
                    list.add(temp);
                    break;
                }
            }
        }

        for (int i = y - 1; i >= 0; i--) {
            Chess temp = ChessboardWarImage.getChess(x, i);
            if (temp == null) {
                list.add(new Point(x, i));
            } else {
                if (temp.getType().equals(chess.getType())) {
                    break;
                } else {
                    list.add(temp);
                    break;
                }
            }
        }
    }

    private static void pao(Chess chess, List<Point> list) {
        int x = chess.getX();
        int y = chess.getY();

        // 中间隔子数量
        int count = 0;

        count = 0;
        for (int i = y - 1; i >= 0; i--) {
            Chess temp = ChessboardWarImage.getChess(x, i);
            if (temp == null) {
                if (count == 0) {
                    list.add(new Point(x, i));
                } else if (count == 1) {

                } else {
                    break;
                }
            } else {
                count++;
                if (count == 2) {
                    if (!temp.getType().equals(chess.getType())) {
                        list.add(temp);
                        break;
                    }
                }
            }
        }

        count = 0;
        for (int i = y + 1; i <= 9; i++) {
            Chess temp = ChessboardWarImage.getChess(x, i);
            if (temp == null) {
                if (count == 0) {
                    list.add(new Point(x, i));
                } else if (count == 1) {

                } else {
                    break;
                }
            } else {
                count++;
                if (count == 2) {
                    if (!temp.getType().equals(chess.getType())) {
                        list.add(temp);
                        break;
                    }
                }
            }
        }

        count = 0;
        for (int i = x - 1; i >= 0; i--) {
            Chess temp = ChessboardWarImage.getChess(i, y);
            if (temp == null) {
                if (count == 0) {
                    list.add(new Point(i, y));
                } else if (count == 1) {

                } else {
                    break;
                }
            } else {
                count++;
                if (count == 2) {
                    if (!temp.getType().equals(chess.getType())) {
                        list.add(temp);
                        break;
                    }
                }
            }
        }

        count = 0;
        for (int i = x + 1; i <= 8; i++) {
            Chess temp = ChessboardWarImage.getChess(i, y);
            if (temp == null) {
                if (count == 0) {
                    list.add(new Point(i, y));
                } else if (count == 1) {

                } else {
                    break;
                }
            } else {
                count++;
                if (count == 2) {
                    if (!temp.getType().equals(chess.getType())) {
                        list.add(temp);
                        break;
                    }
                }
            }
        }
    }
}